import { Module, Logger } from '@nestjs/common';
import { ConfigService, ConfigModule } from '@nestjs/config';
import { AppController } from '../controllers/app.controller';
import { AppService } from '../services/app.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import configs from '@/configs';
import { User } from '@/entities/user.entity';
import { UserView } from '@/entities/user-view.entity';
import { AuthModule } from './auth.module';
import { LogModule } from './log.module';
import { UploadModule } from './upload.module';

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
      load: [configs],
    }),
    TypeOrmModule.forRootAsync({
      useFactory(configService: ConfigService) {
        return {
          type: 'mysql',
          host: configService.get('database.host'),
          port: configService.get('database.port'),
          username: configService.get('database.username'),
          password: configService.get('database.password'),
          database: configService.get('database.database'),
          synchronize: false,
          logging: configService.get('database.logging'),
          // 最大链接数
          poolSize: configService.get('database.poolSize'),
          // 最大超时时间
          maxQueryExecutionTime: configService.get(
            'database.maxQueryExecutionTime',
          ),
          timezone: '+08:00',
          // 失败重连次数
          retryAttempts: 5,
          // 重连延时ms
          retryDelay: 1000,
          entities: [User, UserView],
        };
      },
      inject: [ConfigService],
    }),
    AuthModule,
    LogModule,
    UploadModule
  ],
  controllers: [AppController],
  providers: [AppService, Logger],
})
export class AppModule {}
